package com.album.service;

import java.util.Iterator;

import com.album.R;
import com.album.common.Sharevariable;
import com.album.task.ICallBack;
import com.album.task.Task;
import com.album.task.UploadImageTask;
import com.album.utils.CommonUtil;
import com.album.utils.Config;
import com.album.utils.MyLog;
import com.album.utils.file.FileOperation; 
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class UploadImageService extends Service{
	private final int upload_cameraimage_suceed = 1;
	private final int upload_nitiveimage_suceed = 2;
	private NotificationManager m_NotificationManager;
	private final int notifyID = 1111;
	private Context ctx;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		MyLog.debug(getClass(), "...onCreate...");
		// TODO Auto-generated method stub
		this.ctx=this;
		super.onCreate();
	}
	
	private Handler hanler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case upload_cameraimage_suceed:
				notificationShow();
				break;
			case upload_nitiveimage_suceed:
				notificationShow();
				break;
			default:
				break; 
			}
		}
	};
	
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		String action = intent.getAction();
		if(action.equals(Config.UPLOAD_IMAGE_ACTION)){
			
			Iterator<String> iterator = Sharevariable.imagePaths.iterator();
			while(iterator.hasNext()){
				String imagePath = iterator.next();
				String imageName = FileOperation.getPathName(imagePath);
				MyLog.info(getClass(), "path:"+imagePath+"\t"+"imageName:"+imageName);
				
				Task task = new UploadImageTask(ctx,"18926128667@189.cn",imagePath,new ICallBack() {
					
					@Override
					public void CallBack(String imageUrl, Bitmap bm) { }
					
					@Override
					public void CallBack(String rspString) {
						// TODO Auto-generated method stub
						MyLog.info(getClass(), "login resp:"+rspString);
						hanler.sendEmptyMessage(upload_nitiveimage_suceed);
					}

					@Override
					public void TimeOut() {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void HttpError(int errorCode) {
						// TODO Auto-generated method stub
						
					}
				});
				task.doTask(task);
			}
		}
		else if(action.equals(Config.UPLOAD_CAMERAIMAGE_ACTION)){
			String imagePath = intent.getStringExtra(Config.CAMEAR_IMAGEPATH);
			MyLog.info(getClass(), "imagePath:"+imagePath);
//			// camera
			Task task = new UploadImageTask(ctx,"18926128667@189.cn",imagePath,new ICallBack() {
				
				@Override
				public void CallBack(String imageUrl, Bitmap bm) { }
				
				@Override
				public void CallBack(String rspString) {
					// TODO Auto-generated method stub
					MyLog.info(getClass(), "login resp:"+rspString);
					hanler.sendEmptyMessage(upload_cameraimage_suceed);
				}

				@Override
				public void TimeOut() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void HttpError(int errorCode) {
					// TODO Auto-generated method stub
					
				}
			});
			task.doTask(task);
		}
		
	}

	public void notificationShow() { 
		NotificationManager m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//		Intent localIntent = new Intent(this, SettingPreferences.class);
//	    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		PendingIntent m_PendingIntent = PendingIntent.getActivity(this, 0, null, 0);
		
		Notification notification = new Notification();
		notification.icon=android.R.drawable.ic_menu_upload;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;	//在通知栏上点击此通知后自动清除此通知
//		notification.defaults = Notification.DEFAULT_ALL;
		
		notification.defaults |= Notification.DEFAULT_VIBRATE;	//添加振动
		notification.defaults |= Notification.DEFAULT_LIGHTS;   //添加LED灯提醒
		notification.tickerText = "上传成功";
		notification.setLatestEventInfo(this, "上传成功", null, m_PendingIntent);
		m_NotificationManager.notify(notifyID, notification);
	} 

	private void cancelNotify(){ 
		m_NotificationManager.cancel(notifyID);
		m_NotificationManager.notifyAll();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		m_NotificationManager = null;
	}
}
